<?php

/**
 *   OPENSHOP 程序说明
 * 
 *   @link        http://baison.com.cn
 *   @copyright   Baison, Inc.
 *   @package     OpenShop
 *   @version     $Id: sms_buildin_sign.php,v 1.0 2009/06/17 08:36:58 modified $
 *   @author      FillBag <fillbag@hotmail.com>
 */

define('IN_OS', true);

require(dirname(__FILE__) . '/includes/init.php');
require_once(ROOT_PATH . 'includes/cls_sms_baison.php');

$sms_baison = new sms_baison();

$exc = new exchange($os->table("sms_buildin_info"), $db, 'info_id', 'info_content');

$smarty->assign("act",$_REQUEST['act']);

if ($_REQUEST['act'] == 'edit_info_content')
{

    check_authz_json('edit_info_content');

    $info_id   = intval($_POST['id']);
    
    $info_content = json_str_iconv(trim($_POST['val']));
    

    if ($exc->edit("info_content = '".$info_content."'", $info_id))
    {
        clear_cache_files();
        make_json_result(stripslashes($info_content));
    }
}

if ($_REQUEST['act'] == 'toggle_display')
{

    check_authz_json('toggle_display');

    $info_id = intval($_POST['id']);
    $val = intval($_POST['val']);
    
    $info_id   = intval($_POST['id']);
    
    if ($exc->edit("is_display = '".$val."'", $info_id))
    {
        clear_cache_files();
        make_json_result(stripslashes($val));
    }
}

/*------------------------------------------------------ */
//-- 翻页、搜索、排序
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'query')
{
    $list = $sms_baison->getBuildInfoList('1');
    check_authz_json('query');
    $smarty->assign('buildinSign',$list['item']);
    $smarty->assign('filter',       $list['filter']);
    $smarty->assign('record_count', $list['record_count']);
    $smarty->assign('page_count',   $list['page_count']); 

    $sort_flag  = sort_flag($list['filter']);
    $smarty->assign($sort_flag['tag'], $sort_flag['img']);

    make_json_result($smarty->fetch('sms_bdin_sign_list.tpl'), '',
        array('filter' => $list['filter'], 'page_count' => $list['page_count']));
}

if ($_REQUEST['act'] == 'add')
{
    /* 权限判断 */
    admin_priv('buildin_manage');

    $smarty->assign('ur_here',     '添加内置签名');
    $smarty->assign('action_link', array('text' => '返回签名列表', 'href' => 'sms.php?act=sms_bdin_sign_list'));
    $smarty->assign('form_action', 'insert');
    
    $guide['level_one']=4;
    $guide['level_two']="buildin_sign_info";
    $guide['level_three']="";
    $smarty->assign('guide',$guide);

    assign_query_info();
    $smarty->assign('buildinSign', array('is_display'=>1));
    $smarty->display('index.tpl');
}

if ($_REQUEST['act'] == 'insert')
{
    /* 权限判断 */
    admin_priv('buildin_manage');

    $is_display = isset($_POST['is_display']) ? intval($_POST['is_display']) : 1;
    
    $smarty->assign('ur_here',     '添加内置签名');
    
    /*内置签名内容*/
    if (!empty($_POST['info_content']))
    {
        $_POST['info_content'] = $_POST['info_content'];
    }
    $today  = local_strtotime(local_date('Y-m-d H:i:s'));
    $sql = "INSERT INTO ".$os->table('sms_buildin_info')."(info_content, create_time, info_type,is_display) ".
           "VALUES ('$_POST[info_content]', '$today ', '1', '$is_display')";
    $db->query($sql);
    
    /* 清除缓存 */
    clear_cache_files();

    $link[0]['text'] = '继续添加内置签名';
    $link[0]['href'] = 'sms_buildin_sign.php?act=add';

    $link[1]['text'] = '返回内置签名列表';
    $link[1]['href'] = 'sms.php?act=sms_bdin_sign_list';

    sys_msg('内置签名添加成功！', 0, $link);
}

if ($_REQUEST['act'] == 'edit_buildsms')
{
    /* 权限判断 */
    admin_priv('buildin_manage');

    $is_display = isset($_POST['is_display']) ? intval($_POST['is_display']) : 1;
    
    //$smarty->assign('ur_here',     '添加内置签名');
    
    /*内置签名内容*/
    if (!empty($_POST['info_content']))
    {
        $_POST['info_content'] = $_POST['info_content'];
    }
    $param = "info_content = '$_POST[info_content]', is_display='$is_display'";
    if ($exc->edit($param,  $_POST['info_id']))
    {
        /* 清除缓存 */
        clear_cache_files();

        admin_log($_POST['info_content'], 'edit', 'buildinSign');

        $link[0]['text'] = '返回签名列表';
        $link[0]['href'] = 'sms.php?act=sms_bdin_sign_list&' . list_link_postfix();
        sys_msg('编辑内置签名成功', 0, $link);
    }
    else
    {
        die($db->error());
    }
}

if ($_REQUEST['act'] == 'edit')
{
    /* 权限判断 */
    admin_priv('buildin_manage');

    $smarty->assign('ur_here',     '编辑内置签名');
    $smarty->assign('action_link', array('text' => '返回签名列表', 'href' => 'sms.php?act=sms_bdin_sign_list'));
    $smarty->assign('form_action', 'edit_buildsms');
    
    $sql = "SELECT info_content, is_display ".
           "FROM " .$os->table('sms_buildin_info'). " WHERE info_id='$_REQUEST[id]'";
    $buildinSign = $db->GetRow($sql);
    $guide['level_one']=4;
    $guide['level_two']="buildin_sign_info";
    $guide['level_three']="";
    $smarty->assign('info_id',$_REQUEST[id]);
    $smarty->assign('buildinSign',$buildinSign);
    $smarty->assign('guide',$guide);
    assign_query_info();
    //$smarty->assign('buildinSign', array('is_display'=>1));
    $smarty->display('index.tpl');
}

if ($_REQUEST['act'] == 'remove')
{
    check_authz_json('buildin_remove');

    $info_id = intval($_GET['id']);

    $sql = "DELETE FROM " .$os->table('sms_buildin_info'). " WHERE info_id = '$info_id'";
    $res = $db->query($sql);
    
    admin_log('', 'remove', '内置签名');

    $url = 'sms_buildin_sign.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);
    os_header("Location: $url\n");
    exit;
}

if ($_REQUEST['act'] == 'batch_remove')
{
    check_authz_json('buildin_batch_remove');

    if (!isset($_POST['checkboxes']) || !is_array($_POST['checkboxes']))
    {
        sys_msg('没有选择任何内置签名', 1);
    }

    $count = 0;
    foreach ($_POST['checkboxes'] AS $key => $id)
    {
        if ($exc->drop($id))
        {
            $name = $exc->get_name($id);
            admin_log(addslashes($name),'remove','buildinSign');
            $count++;
        }
    }
    $lnk[] = array('text' => '返回内置签名列表', 'href' => 'sms.php?act=sms_bdin_sign_list');
    sys_msg(sprintf('您已经成功删除 %d 条内置签名', $count), 0, $lnk);
}

if ($_REQUEST['act'] == 'search_buildinsign')
{
    check_authz_json('search_buildinsign');

    include_once(ROOT_PATH . 'includes/cls_json.php');

    $where = ' WHERE is_display = 1 AND info_type = 1 ';
    
    $json   = new JSON;
        /* 取得数据 */
    $sql = 'SELECT s.info_id, LEFT(s.info_content,20) info_content '.
           'FROM ' . $GLOBALS['os']->table('sms_buildin_info') . ' AS s ' . $where .
           'LIMIT 50';
    $row = $GLOBALS['db']->getAll($sql);

    if (empty($row))
    {
        $row[0] = array(
            'info_id'   => 0,
            'info_content' => ''
        );
    }

    make_json_result($row);
}

if ($_REQUEST['act'] == 'getInfoContent')
{
    check_authz_json('getInfoContent');

    include_once(ROOT_PATH . 'includes/cls_json.php');

    $id = intval($_GET['id']);
    
    $where = ' WHERE info_id = '.$id;
    
    $json   = new JSON;
        /* 取得数据 */
    $sql = 'SELECT info_content '.
           'FROM ' . $GLOBALS['os']->table('sms_buildin_info') . ' AS s ' . $where;
    $row = $GLOBALS['db']->getAll($sql);

    make_json_result($row);
}
?>